package com.dayouzi.crawler_data.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 根据mybatis.xml中配置的不同的environment创建对应的SqlSessionFactory
 */
@Slf4j
final class DataSourceSqlSessionFactory {
    private static final String CONFIGURATION_PATH = "mybatis.xml";

    private static final Map<DataSourceEnvironment, SqlSessionFactory> SQLSESSIONFACTORYS
            = new HashMap<DataSourceEnvironment, SqlSessionFactory>();


    /**
     * 根据指定的DataSourceEnvironment获取对应的SqlSessionFactory
     *
     * @param environment 数据源environment
     * @return SqlSessionFactory
     */
    public static SqlSessionFactory getSqlSessionFactory(DataSourceEnvironment environment) {

        SqlSessionFactory sqlSessionFactory = SQLSESSIONFACTORYS.get(environment);
        if (null != sqlSessionFactory)
            return sqlSessionFactory;
        else {
            InputStream inputStream = null;
            try {
                inputStream = Resources.getResourceAsStream(CONFIGURATION_PATH);
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, environment.name());

                log.info("Get {} SqlSessionFactory successfully.", environment.name());
            } catch (IOException e) {
                log.warn("Get {} SqlSessionFactory error.", environment.name());
                log.error(e.getMessage(), e);
            } finally {
                IOUtils.closeQuietly(inputStream);
            }

            SQLSESSIONFACTORYS.put(environment, sqlSessionFactory);
            return sqlSessionFactory;
        }
    }

}